<style>
    .page {
        min-height: 100vh;
        background: #f5f5f5;
        padding-bottom: 40px;
    }
    .g-address {
        background: #fff;
        margin-bottom: 10px;
    }
    .g-line1 {
        padding: 20upx;
        display: flex;
        justify-content: space-between;
    }
    .g-line2 {
        position: relative;
        padding: 0 20upx 20upx;
    }
    
    .g-line3 .btn {
        display: inline-flex;
        align-items: center;
        margin-left: 10px;
    }
    .g-line3 image {
        width: 44upx;
		height: 44upx;
        margin-right: 5px;
    }
    .add {
        position: fixed;
        left: 0;
        bottom: 0;
        padding: 25px 0;
        width: 100%;
        background: #d71611;
        color: #fff;
        text-align: center;
		font-size: 32upx;
		z-index: 10;
    }
</style>

<template>
	<view class="page">
        <view class="g-address" v-for="(item,index) in data" :key="index" >
            <view class="g-line1" @tap="addressTap" :data-id="item.id">
                <text>{{item.realname}}</text>
                <text>{{item.mobile}}</text>
            </view>
            <view class="g-line2 f-border1px" @tap="addressTap" :data-id="item.id">{{item.province_text+item.city_text+item.zone_text+item.address}}</view>
            <view class="g-line3 g-line1">
                <label @tap="toggleDefault" :data-index="index"><radio color="#d71611" :checked="item.is_default"></radio><text>默认地址</text></label>
                <view>
                    <navigator open-type="navigate" class="btn" :url="'/pages/user/settings/address/edit_address?id='+item.id">
                        <image src="../../../../static/icons/user/edit.png" mode="widthFix"></image>
                        <text>编辑</text>
                    </navigator>
                    <button class="btn" @tap="delAddress" :data-id="item.id">
                        <image src="../../../../static/icons/user/delete.png" mode="widthFix"></image>
                        <text>删除</text>
                    </button>
                </view>
            </view>
        </view>
        <view class="m-loading" v-if="bIsShowLoading">
            <image src="../../../../static/images/loading.gif" mode="widthFix"></image>
            <text>加载中</text>
        </view>
        <navigator open-type="navigate" class="add" url="/pages/user/settings/address/add_address">添加新地址</navigator>
	</view>
</template>

<script>
    import henglang from '@/common/common.js';
	export default {
		data() {
			return {
                nSelected: 0,
				data: [],
                bIsShowLoading: false,
                goods_id: '',
                province_id: '',
                num: '',
				param:'',
				settype:'',
				agent: 0,	// 是否从创客过来
			};
		},
        methods: {
            toggleDefault(e) {
                let index = e.currentTarget.dataset.index;
                let that = this;
                
                this.data.forEach((item)=>{
                    item.is_default = 0;
                })
                
                this.data[index].is_default = 1;
                this.bIsShowLoading = true;
				
                henglang.post('member/addressModify',this.data[index],true,function(res){
                	that.bIsShowLoading = false;
                }); 
            },
            delAddress(e) {// 删除
                let that = this;
                let id = e.currentTarget.dataset.id;
                this.bIsShowLoading = true;
				henglang.get('member/addressDel',{id: id},true,function(res){
					let origin = that.data;
					origin.forEach((item,index)=>{
					    if (item.id == id) {
					        origin.splice(index,1);
					    }
					})
					that.data = origin;
					henglang.showToast(res.data.msg,500);
					that.bIsShowLoading = false;
				});
            },
            addressTap(e) {
                let address_id = e.currentTarget.dataset.id;
				let that = this;
				var url = ''
				if (that.settype == 'setting') return false
                if (that.type=="payment") {
                    url = '/pages/order/payment?'
                }else if(that.type == "entrust"){
					url = '/pages/transfer/entrust_confirm?'
				}else if(that.type == "pawn"){
					url = '/pages/pawn/shipment?'
				}else if(that.type == "lottery"){
					uni.setStorage({
						key:'address_id',
						data:address_id
					})
					uni.navigateBack()
					return false
				}else{
					url = '/pages/jewel/order/orderinfo?'
				}
				if(that.agent) url = '/pages/jewel/agent/order/orderinfo?';
				
				uni.redirectTo({url: url + that.param + '&address_id=' + address_id + '&delta=2'});
				
            }
        },
        onShow() {
            let that = this;
			that.bIsShowLoading = true
            henglang.get('member/address',{},true,function(res){
				that.bIsShowLoading = false
				if(res.data.code == 0){
					that.data = res.data.data;
				}
            });
        },
        onLoad(options) {
			let that = this
			that.settype = options.settype
			that.type = options.type;
			if(options.promotion_id){
				that.promotion_id = options.promotion_id;
				that.goods_id = options.goods_id;
				that.num = options.num;
			}
			if(options.agent) that.agent = 1;
			for (let i in options) {
				that.param+= i + '=' + options[i] + '&';
			}
        }
	}
</script>

